package com.xhui.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xhui.reggie.common.BaseContext;
import com.xhui.reggie.common.R;
import com.xhui.reggie.entity.AddressBook;
import com.xhui.reggie.service.AddressBookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author XHui
 * Date 2023/1/20 11:25
 * Version 1.0
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;

    /**
     * 查询当前用户的所有收货地址
     * @return 所有收货地址
     */
    @GetMapping("/list")
    public R< List<AddressBook> > list(){
        // 通过已封装好的工具类(底层是基于session)获取到当前用户的id
        Long currentUserId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // eq 是在 where 后添加条件 如： where address.user_id = ...
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId, currentUserId);
        List<AddressBook> list = addressBookService.list(addressBookLambdaQueryWrapper);
        System.out.println("list = " + list);
        return R.success(list);
    }

    /**
     * 根据id查询详细信息
     * @param id 地址簿某个地址具体Id
     * @return 地址详细信息
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id){
        AddressBook queriedAddressBook = addressBookService.getById(id);
        return R.success(queriedAddressBook);
    }

    /**
     * 修改收货地址信息
     * @return 是否成功
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        boolean flag = addressBookService.updateById(addressBook);
        return flag ? R.success("修改成功"): R.success("修改失败");
    }

    /**
     * 新增收货地址信息
     * @param addressBook 地址簿信息
     * @return 是否成功
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        //
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
       return R.success("收货地址保存成功");
    }

    /**
     * 查询默认地址
     * @return 返回已登录用户的默认地址
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        // 查询指定用户的默认地址
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook queriedAddressBook = addressBookService.getOne(queryWrapper);

        return queriedAddressBook != null ? R.success(queriedAddressBook): R.error("查询默认地址失败");
    }

    /**
     * 设置某个地址为默认(若存在多个地址，还需确保)
     * @param addressBook 地址信息
     * @return 是否成功
     */
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return R.success("修改默认地址成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        System.out.println("ids = " + ids);
        boolean flag = addressBookService.removeByIds(ids);
        return flag ? R.success("修改成功"): R.error("修改失败");
    }
}
